Hello and welcome back in the session about for loops of follow ups.
One of the most important and most heavily used concepts and programming.
And before we learned about lists and lists that contain us of many other objects so like a backpack
that you put in things like a sandwich or a bottle of water or an extra T-shirt before you go hiking.
And now let's assume that it's raining in your backpack and all elements get wet and what do I typically
do you unpack each element and take an action on it.
So in this case you are trying each element in the Sun and others call it an elemental ICE operation.
But earlier we learned that however element vice operations are not a stand out behavior of lists.
So let's create a list with four elements.
One two three four.
And now our attention is to multiply each element by two or so an element y's operation but you already
know if you just put added times to we get a multiple of the list that set off so actually we created
two copies of L and then we concatenated it but we didn't get an element y's operation.
But now what should we do if we want to f an element y's operation and they are the far keyword comes
into place.
So we are saying for j in lists that we can also say for element in lists
we can name the each element of the list with the name we want.
We can say element or i or j and that's important so we can say for element for each element and list
and then we have to make a colon.
And if you print that and if you press return then we automatically pass and make serial indent and
then we say print the element so we can see here we iterated over all elements and we printed all elements
so we get one two three four.
And of course we can do more than just printing we can say okay each element we want to multiply by
two and then print it.
We can also do that
and also we can say that we name each element either Multiply each side by two and then rename the multiplied
element as J and then print Chase so we get the same result of course.
And before we learned the function length so with the function length we can determine the amount of
elements and unless we can replicate this process by a few lines of code so we can say count equals
zero and four element or four under slash in list please increase my count
sum count as far.
So we just determined the length of our list L four elements.
Again I one wanted to highlight two things so you need the colon here.
So it's the same with an if statement if you forget the colon you get an error message and we need to.
The intent here.
So if you forget the intent we also gotten our message
though this is still the right structure.
So before I said we can iterate over all its rebels Soviet era appearance objects that contain many
elements and also we learned that a string also is a contain of many elements.
So for example the string dock contains three characters d o a..
And they let us create the rival D equal stock and then we can also iterate over D and print all single
element D O T.
And we can also print each character as an upper character so we can say for each character in D please
print me in the upper character and we get D O T and we can also determine the length of our string
and of course it's three
at this point I want to introduce the range object and arrange object is also an interval so we can
iterate over all elements and in this example the range object stops integers from 1 including until
5 excluding So if you're under so you only get here the range object from 1 to 5 and we can also check
the tape of the range object its range object and when I say the range object stops then the elements
it's the action of really storing it it just makes itself ready to give me all elements if I explicitly
want to have them so I can explicitly call the range object by creating a list
and then the list is containing all four elements of our range object we can also iterate over arrange
objects or same with the list so we can say for Aeon range 1 to 5 please print me all elements and we
get 1 2 3 4.
So the last element excluding So from 1 including 2 5 excluding and this is actually the same behavior
as the list so before we had our list.
1 2 3 4 and we iterated over it and we got all the elements.
So what's the difference to our list.
Well a list once created it stops all elements of the list at the same time.
And of course we can iterate over all elements but the range object arrange object never stops all elements
of it at the same time so we can iterate element wise of all elements of the Range object and Python
internally stores only the actual element so we iterate over one element to something that this element.
Now after we finish and want to go through the next element then Python drops out our actual element
and takes it out of memory and then takes the next elements or Python never stops more than one element
at the same time with the Range object and that's quite memory efficient.
So if you assume we don't have an element or Range object with four elements but with one million elements
so it makes a difference if we store million elements like only one one element.
All right.
So let's go on with the Range object and of course we can create 5 iterations so we can say 40 in range.
5 and 5 stands for the last element.
So excluding by default our starting element is 0.
So here we get all elements from 1 to 4 and we can also do it explicitly we can say we want the Range
object from 0 including 2 10 excluding but only every third element so we can also define this one here
and then we get 0 0 3 6 and 9.
Of course we can do more than just printing we can for example multiply each element in our range object
and print it out and then we get all multiplied elements from 1 to 10 including let's go to our very
common workflow.
So we want to store the crowd numbers from 1 to 10 in our list and therefore we create an empty list.
L We iterate over the Range object the from 1 to 10 including we calculate the square number for each
element and call its career and then we append each scrap number to the list ever so let's see what
we get here.
So we get an list with ask that numbers from 1 to 10 and that's also quite a concept.
Call it the list comprehension which does actually the same top.
So we can say here we create here.
And the spare brackets that we create a list here.
We can say please for all elements in our range Object 1 till 10 please give us the element to the power
of 2 and please start in a list and it's a case execute the sale and they can see if we get the same
result as before.
So we reduced three lines or four lines of code to only one line of code with the list comprehension.
All right so let's go on with the number case study and let's count on numbers between 0 and ten thousand
that are divisible by 2 and 5 without remainder.
So we can say for Aeon range ten thousand one so we want to include ten thousand and if one model or
two is zero.
So if one is divisible by 2 without remainder and one model of 5 equals zero then increase our count
by 1.
So first here at the beginning we introduce the account equal equals zero and end for each element in
our range object that fulfills both conditions we have an end statement please increase our count and
then we get 1001 elements that fulfill the conditions so obviously a zero for first our condition and
then 10 20 30 and ten thousand so each elements that are actually divisible by 10.
All right so that's for the time being and the next session we will have a look at the key arguments
are key words continue break and pass.
So see you there by.